package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhangmeng
 * @description 菜单项控制器
 * @date 2019/9/18
 **/
@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {

    @DubboReference
    private MenuService menuService;

    /**
     * 添加
     *
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        log.info("[菜单项-添加]data:{},menu:{}", menu);
        //RPC请求添加
        menuService.add(menu);
        return new Result(true, MessageConst.ADD_MENU_SUCCESS);
    }


    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[菜单项-分页查询]data:{}", queryPageBean);
        //rpc查询结果
        PageResult pageResult = menuService.pageQuery(queryPageBean);
        //构造返回对象
        return new Result(true,MessageConst.ACTION_SUCCESS,pageResult);

    }
    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        log.info("[菜单项-根据id查询]id:{}", id);

        //rpc 调用业务处理
        Menu menu = menuService.findById(id);
        //封装返回值
        return new Result(true, MessageConst.QUERY_MENU_SUCCESS, menu);

    }


    /**
     * 编辑
     *
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        log.info("[菜单项-编辑]data:{}",menu);

        //RPC请求添加
        menuService.edit(menu);
        return new Result(true, MessageConst.EDIT_MENU_SUCCESS);

    }


    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        log.info("[菜单项-根据id删除]id：{}",id);
        //RPC调用处理业务
        menuService.delete(id);
        return new Result(true,MessageConst.DELETE_MENU_SUCCESS);
    }
}
